package leetcode_core.leetcode_2;

import org.junit.Test;

public class FindMedianSortedArrays {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //首先判断奇数和偶数,以便确定遍历指针的目的地点
        int totalLen = nums1.length+nums2.length;
        //样例分析:对于奇数长度的数组而言,nums1=[1,3],nums2=[2],merge=[1,2,3]
        //其要找的数的下标是1,中心点位置是int(3/2)
        //其总长度是3,一共是遍历len/2+1次(指指针移动)
        //对于偶数长度的数组而言,nums1=[1,2],nums2=[3,4],merge=[1,2,3,4]
        //其总长度为4,要找到的中心点位置是(4/2)-1和(4/2),一共是需要遍历len/2+1次
        int p1 = 0,p2 = 0;
        int left=-1,right=-1;
        //p1遍历nums1数组,p2遍历nums2数组
        for(int i = 0 ;i<totalLen/2+1;i++){
            //一共遍历这么多次,如果p1已经超过了下标,那么直接对nums2进行计数
            System.out.println("p1="+p1+",p2="+p2);
            System.out.println("left="+left+",right="+right);
            left = right;
            if( p2 >= nums2.length|| (p1<nums1.length  && nums1[p1]<=nums2[p2])){
                right = nums1[p1++];
            }else {
                right = nums2[p2++];
            }
        }
        if(totalLen%2 == 0){
            return (double) (left+right)/2;
        }
        return right;
    }
    @Test

    public void test(){
        System.out.println(findMedianSortedArrays(new int[]{2}, new int[]{}));
        System.out.println(findMedianSortedArrays(new int[]{2},new int[]{1, 3}));
    }

}
